package com.controller;


import com.github.pagehelper.PageInfo;
import com.pojo.Sitepoint;
import com.service.SiteService;
import com.vo.Result;
import com.vo.SitePointVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/Site")
public class SitePointController {
    @Resource
    private SiteService siteService;

//巡查点
    @RequestMapping("/SitePoint/index")
    public String toSitepoint(){
        return "/Site/SitePoint";
    }


    @RequestMapping("/SitePoint/show")
    @ResponseBody
    public Result select(@RequestBody SitePointVo sitePointVo){
        PageInfo<Sitepoint> sitepointPageInfo = siteService.selectAllSitepoint(sitePointVo);
        return Result.success(sitepointPageInfo);
    }
    @RequestMapping("/SitePoint/show2")
    @ResponseBody
    public Result select2(){
        List<Sitepoint> selectsitepoint = siteService.selectsitepoint();
        return Result.success(selectsitepoint);
    }

    @RequestMapping("/SitePoint/delete")
    @ResponseBody
    public Result delete(Integer id){
        try {
            Integer selectsitebyid = siteService.selectsitebyid(id);
            if (selectsitebyid==2){
                siteService.deleteSitePointbyid(id);
                return Result.success("删除成功");
            }else {
                return Result.success("该巡检点正在使用，无法删除");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("删除失败");
        }
    }

    @RequestMapping("/SitePoint/add")
    @ResponseBody
    public Result add(Sitepoint sitepoint){
        try {
            sitepoint.setRealsitetime(" ");
            sitepoint.setAttachpath(" ");
            siteService.addSitepoint(sitepoint);
            return Result.success("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("添加失败");
        }
    }

    @RequestMapping("/SitePoint/update")
    @ResponseBody
    public Result update(Sitepoint sitepoint){
        try {
            siteService.updateSitepoint(sitepoint);
            return Result.success("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("修改失败");
        }
    }
}
